package com.shxt.hotel.background;
import com.shxt.hotel.util.*;
import com.shxt.hotel.conf.*;
import java.util.Map;
import java.util.List;

public class Staff {
    
    public static final int SUCCESS = 0;
    public static final int STAFF_NAME_ERROR = 1;
    public static final int PASSWORD_ERROR = 2;
    
    private static final String TABLE_NAME = "staff";
    
    private int staff_id = -1;
    DBManager dbm;
    
    public Staff()
    throws Exception{
        try{
            dbm = new DBManager(Config.DB_NAME, Config.DB_USERNAME, Config.DB_PASSWORD);
        }catch(Exception e){
            throw e;
        }
    }
    
    public int login(String staffName, String password)
    throws Exception{
        try{
            String sql = "select * from " + TABLE_NAME + " where staff_name = '" + staffName + "'";
            List<Map<String, Object>> staffList = dbm.executeQuery(sql);
            
            if(staffList.size() == 0){
                return Login.STAFF_NAME_ERROR;
            }
            if(!staffList.get(0).get("password").equals(password)){
                return Login.PASSWORD_ERROR;
            }
            
            this.staff_id = (int)staffList.get(0).get("id");
            return Login.SUCCESS;
            
        }catch(Exception e){
            throw e;
        }
    }
    
    public int getStaffId()
    throws Exception{
        try{
            return this.staff_id;
        }catch(Exception e){
            throw e;
        }
    }
    
    public List<Map<String, Object>> getStaffInfo(int id)
    throws Exception{
        try{
            String sql = "select * from " + Staff.TABLE_NAME + " where id = " + id;
            return dbm.executeQuery(sql);
        }catch(Exception e){
            throw e;
        }
    }
    
    public void close()
    throws Exception{
        try{
            dbm.close();            
        }catch(Exception e){
            throw e;
        }
    }
    
}
